import React from "react";
import { Typography, Button } from "antd";
import { EyeOutlined, FileTextOutlined } from "@ant-design/icons";
import markdownit from "markdown-it";
import type { BubbleProps } from "@ant-design/x";
import { Answer, AnswerSummary, Chat, MessageItem } from "./types";
import FlowContentRenderer from "./FlowContentRenderer";
import { FlightOption, SimplifiedFlightInfo } from "../../services/apiService";

// 配置 markdown-it 以支持 HTML 和更好的渲染
const md = markdownit({
  html: true, // 允许 HTML 标签
  breaks: true, // 转换换行符
  linkify: true, // 自动转换链接
  typographer: true, // 启用一些语言中性的替换 + 引号美化
});

// 提取 flow_html 内容的函数
export const extractFlowHtmlContent = (content: string): string | null => {
  const regex = /<flow_html>([\s\S]*?)<\/flow_html>/;
  const match = content.match(regex);
  return match ? match[1].trim() : null;
};

// 提取 flow_product_id 内容的函数
export const extractFlowProductId = (content: string): string | null => {
  const regex = /<flow_product_id>([\s\S]*?)<\/flow_product_id>/;
  const match = content.match(regex);
  return match ? match[1].trim() : null;
};

// 提取 flow_summary_result 内容的函数
export const extractFlowSummaryResult = (content: string): string | null => {
  const regex = /<flow_summary_result>([\s\S]*?)<\/flow_summary_result>/;
  const match = content.match(regex);
  return match ? match[1].trim() : null;
};

// 移除 flow_html 标签的函数
export const removeFlowHtmlTags = (content: string): string => {
  const regex = /<flow_html>[\s\S]*?<\/flow_html>/g;
  return content.replace(regex, "");
};

// 移除 flow_product_id 标签的函数
export const removeFlowProductIdTags = (content: string): string => {
  const regex = /<flow_product_id>[\s\S]*?<\/flow_product_id>/g;
  return content.replace(regex, "");
};

// 移除 flow_summary_result 标签的函数
export const removeFlowSummaryResultTags = (content: string): string => {
  const regex = /<flow_summary_result>[\s\S]*?<\/flow_summary_result>/g;
  return content.replace(regex, "");
};

// 移除所有 flow 标签的函数
export const removeAllFlowTags = (content: string): string => {
  let cleanContent = removeFlowHtmlTags(content);
  cleanContent = removeFlowProductIdTags(cleanContent);
  cleanContent = removeFlowSummaryResultTags(cleanContent);
  return cleanContent;
};

// 创建带有 flow_html、flow_product_id 和 flow_summary_result 支持的渲染函数
export const createMarkdownRenderer = (
  onFlowHtmlClick: (content: string) => void,
  onFlowProductIdClick?: (productId: string, flightInfo?: FlightOption, flightList?: FlightOption[]) => void,
  onSummaryClick?: (content: string, type: "html" | "product",
    metadata: { answerId: string, messageItemId: string }
  ) => void,
  onSummaryResultClick?: (summaryContent: string) => void,
  onSaveMultilang?: (summaryContent: string, originalContent: string, productId?: string) => void, // 新增
) => {
  return (answer: Answer) => {
    return React.createElement(FlowContentRenderer, {
      answer,
      onFlowHtmlClick,
      onFlowProductIdClick,
      onSummaryClick,
      onSummaryResultClick,
      onSaveMultilang, // 新增
    });
  };
};

export const generateId = (type: string): string => {
  return `${type}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
};

export const createAnswer = (messageItem: MessageItem) => {
  const answer = {
    messageItemId: messageItem.id,
    id: generateId("answer"),
    display: "",
    think: "",
    metadata: {},
    humanInputs: [],
    summaries: [],
    status: "pending" as "pending",
    date: new Date(),
  };
  messageItem.answers.push(answer);
  messageItem.answer_length += 1;
  return messageItem.answers[messageItem.answers.length - 1];
};

export const createMessageItem = (question: string): MessageItem => {
  return {
    id: generateId("message"),
    type: "question",
    question: question,
    answers: [],
    answer_length: 0,
    date: new Date(),
  };
};

export const createChat = (userId: string): Chat => {
  return {
    chatId: generateId("chat"),
    userId,
    messageItems: [],
    metadata: {},
  };
};

export const createAnswerSummary = (
  answerId: string,
  messageItemId: string): AnswerSummary => {
  return {
    answerId,
    messageItemId,
    id: generateId("answerSummary"),
    display: "",
    think: "",
    date: new Date(),
  };
};